﻿namespace Baggr.ClientSite.Models
{
    using System;
    using System.IO;

    public static class ProfileHelper
    {
        public static T UpdateProfile<T>(this System.Web.Profile.ProfileBase profile, string name, T newValue)
            where T: class
        {
            if (profile == null)
                throw new ArgumentNullException("profile");
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");

            if (newValue == null)
            {
                var result = (T) profile[name];
                if(result == null)
                    throw new InvalidDataException("Profile default of " + name + " value is undefined");
                return result;
            }

            profile[name] = newValue;
            return newValue;
        }
    }
}
